package br.com.sig.financeiro.frete.business.model.bo;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.enterprise.event.Observes;
import javax.inject.Named;

import br.com.sig.business.exception.BaseException;
import br.com.sig.business.model.BaseService;
import br.com.sig.financeiro.frete.business.event.ContabilizarFreteEvent;
import br.com.sig.financeiro.frete.business.event.PagarFreteEvent;
import br.com.sig.financeiro.frete.business.event.RegistrarFreteEvent;
import br.com.sig.financeiro.frete.business.model.entity.Frete;
import br.com.sig.financeiro.frete.business.model.enumeration.NaturezaDoFrete;

@Named
@Stateless
public class FreteBO extends BaseService<Frete> {

	private static final long serialVersionUID = -591527223199693486L;
	
	@Override
	public void persist(Frete frete) throws BaseException {
		super.persist(frete);
		
		if (frete.getNatureza().equals(NaturezaDoFrete.EMPRESA)) {
			beanManager.fireEvent(new RegistrarFreteEvent(frete));
			beanManager.fireEvent(new ContabilizarFreteEvent(frete));
		}
	}
	
	public void pagar(@Observes PagarFreteEvent event) throws BaseException {
		Date dataDePagamento = event.getDataDePagamento();
		List<Frete> fretesAPagar = event.getFretes();
		
		for (Frete frete : fretesAPagar) {
			frete.setDataDePagamento(dataDePagamento);
			update(frete);
		}
	}	
	
}